<?php
class GestionUtilisateurs extends BaseCtrl{
	function __construct()
	{
		parent::__construct();
		$this->load->library('jsUtils');
	}
	
	public function index(){
		$this->refresh();
	}
	
	public function refresh(){
		
		$this->jsutils->getAndBindTo(".delete", "click", "/wikiPro/gestionUtilisateurs/delete", "body");
		$this->jsutils->getAndBindTo("#add", "click", "/wikiPro/gestionUtilisateurs/add", "body");
		$this->jsutils->compile();
		$this->loadUtils();
		$this->loadGroupes();
		$this->loadMondes();
		$this->load->view('v_gestion_utilisateurs', array('utilisateurs'=>$this->loadUtils(),'groupes'=>$this->loadGroupes(),'mondes'=>$this->loadMondes()));
	}
	
	public function loadUtils(){
		$query = $this->doctrine->em->createQuery("SELECT u FROM Utilisateur u join u.groupe c");
		$users = $query->getResult();
		return $users;
	}
	
	public function loadGroupes(){
		$query = $this->doctrine->em->createQuery("SELECT g FROM Groupe g");
		$groupes = $query->getResult();
		return $groupes;
	}
	
	public function loadMondes(){
		$query = $this->doctrine->em->createQuery("SELECT m FROM Monde m");
		$mondes = $query->getResult();
		return $mondes;
	}
	
	public function delete($param){
		$id=str_replace("utilisateur", "", $param[6]);
		$query = $this->doctrine->em->createQuery("SELECT u FROM Utilisateur u WHERE u.id=".$id);
		$user = $query->getSingleResult();
		$this->doctrine->em->remove($user);
		$this->doctrine->em->flush();
		$this->refresh();
		
		}
	
	public function add(){
		$this->load->helper(array('form', 'url'));
		$this->load->library('form_validation');
		if ($this->form_validation->run() == FALSE){
			$this->load->view('v_gestion_utilisateurs');
			}else{
			$this->submit_add(array("groupe_id"=>$_POST["groupe"],"monde_id"=>$_POST["monde"],"login"=>$_POST["login"],"password"=>$_POST["password"],"nom"=>$_POST["nom"],"prenom"=>$_POST["prenom"],"mail"=>$_POST["email"]));
		}
	}
	
	public function submit_add($groupe_id,$monde_id,$login,$password,$nom,$prenom,$mail){
		
			$user = new Utilisateur();
			
			$user->setGroupe($groupe_id);
			$user->setMonde($monde_id);
			$user->setLogin($login);
			$user->setPassword($password);
			$user->setNom($nom);
			$user->setPrenom($prenom);
			$user->setMail($mail);
			
			$this->doctrine->em->persist($user);
			$this->doctrine->em->flush();
	}

}
	
	

	
	

?>